<?php

// PHP雪里开轻量级报修系统 V1.0Beta
// 演示地址: http://baoxiu.chalide.cn
// 更新关注: /weivote
// 文件路径: index.php
// 文件大小: 11640 字节
// 最后修改时间: 2025-06-14 08:24:20
// 作者: yujianyue
// 邮件: 15058593138@qq.com
// 版权所有,保留发行权和署名权

/**
 * 本文件功能：系统入口页面
 * 版权声明：保留发行权和署名权
 * 作者信息：功能反馈:15058593138@qq.com(手机号同微信)
 */

// 检查是否已安装
if (!file_exists('inc/conn.php')) {
    header('Location: install.php');
    exit;
}

// 引入必要文件
require_once 'inc/conn.php';
require_once 'inc/pubs.php';

// 启动会话
session_start();

// 检查是否已登录
if (isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])) {
    // 根据用户类型跳转到对应页面
    switch ($_SESSION['user_type']) {
        case '系统':
            header('Location: sys.php');
            break;
        case '后勤':
            header('Location: sus.php');
            break;
        case '学生':
            header('Location: stu.php');
            break;
        default:
            // 未知用户类型，注销并重新登录
            session_destroy();
            header('Location: index.php');
            break;
    }
    exit;
}

// 处理登录表单提交
if (isset($_POST['act']) && $_POST['act'] === 'login') {
    // 获取表单数据
    $username = isset($_POST['username']) ? trim($_POST['username']) : '';
    $password = isset($_POST['password']) ? trim($_POST['password']) : '';
    
    // 简单验证
    if (empty($username) || empty($password)) {
        $error_msg = '请输入用户名和密码';
    } else {
        // 连接数据库
        $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        
        // 检查连接
        if ($mysqli->connect_error) {
            $error_msg = '数据库连接失败: ' . $mysqli->connect_error;
        } else {
            // 设置字符集
            $mysqli->set_charset('utf8mb4');
            
            // 查询用户
            $table = DB_PREFIX . 'stux';
            $sql = "SELECT id, realname, password, type, status FROM {$table} WHERE xuehao = ?";
            $stmt = $mysqli->prepare($sql);
            
            if ($stmt) {
                $stmt->bind_param('s', $username);
                $stmt->execute();
                $stmt->bind_result($id, $realname, $db_password, $type, $status);
                
                if ($stmt->fetch()) {
                    // 检查密码
                    if (md5($password) === $db_password) {
                        // 检查状态
                        if ($status == 1) {
                            // 记录会话
                            $_SESSION['user_id'] = $id;
                            $_SESSION['user_name'] = $realname;
                            $_SESSION['user_type'] = $type;
                            
                            // 根据用户类型跳转
                            switch ($type) {
                                case '系统':
                                    header('Location: sys.php');
                                    break;
                                case '后勤':
                                    header('Location: sus.php');
                                    break;
                                case '学生':
                                    header('Location: stu.php');
                                    break;
                            }
                            exit;
                        } else {
                            $error_msg = '账号已被禁用';
                        }
                    } else {
                        $error_msg = '密码错误';
                    }
                } else {
                    $error_msg = '用户不存在';
                }
                
                $stmt->close();
            } else {
                $error_msg = '系统错误: ' . $mysqli->error;
            }
            
            $mysqli->close();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简易报修系统 - 登录</title>
    <style>
        * {box-sizing: border-box;}
        body {font-family: "Microsoft YaHei", Arial, sans-serif; margin: 0; padding: 0; background: #f5f5f5; color: #333; line-height: 1.5;}
        .login-page {display: flex; justify-content: center; align-items: center; min-height: 100vh; padding: 20px;}
        .login-container {width: 100%; max-width: 400px; background: #fff; padding: 30px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);}
        .login-title {text-align: center; margin-bottom: 30px; font-size: 24px; color: #3f51b5;}
        .login-form {margin-bottom: 20px;}
        .form-group {margin-bottom: 20px;}
        .form-label {display: block; margin-bottom: 8px; font-weight: bold;}
        .form-control {display: block; width: 100%; height: 40px; padding: 8px 12px; font-size: 16px; border: 1px solid #ddd; border-radius: 4px;}
        .form-control:focus {border-color: #3f51b5; outline: 0; box-shadow: 0 0 0 2px rgba(63,81,181,0.2);}
        .login-btn {display: block; width: 100%; height: 42px; background: #3f51b5; color: #fff; border: none; border-radius: 4px; font-size: 16px; cursor: pointer;}
        .login-btn:hover {background: #303f9f;}
        .login-links {text-align: center; margin-top: 20px; font-size: 14px;}
        .login-links a {color: #3f51b5; text-decoration: none; margin: 0 10px;}
        .login-links a:hover {text-decoration: underline;}
        .error-msg {background: #ffebee; color: #c62828; padding: 10px; border-radius: 4px; margin-bottom: 20px;}
        .user-type {display: flex; margin-bottom: 20px;}
        .type-item {flex: 1; text-align: center; padding: 10px; border: 1px solid #ddd; cursor: pointer; border-radius: 0;}
        .type-item:first-child {border-radius: 4px 0 0 4px;}
        .type-item:last-child {border-radius: 0 4px 4px 0;}
        .type-item.active {background: #3f51b5; color: #fff; border-color: #3f51b5;}
        footer {text-align: center; padding: 20px; color: #666; font-size: 14px;}
    </style>
</head>
<body>
    <div class="login-page">
        <div class="login-container">
            <h1 class="login-title">简易报修系统</h1>
            
            <?php if (isset($error_msg)): ?>
            <div class="error-msg"><?php echo htmlspecialchars($error_msg); ?></div>
            <?php endif; ?>
            
            <form class="login-form" method="post" action="">
                <input type="hidden" name="act" value="login">
                
                <div class="user-type">
                    <div class="type-item active" data-value="学生">学生</div>
                    <div class="type-item" data-value="后勤">后勤</div>
                    <div class="type-item" data-value="系统">系统</div>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="username">学号/工号</label>
                    <input class="form-control" type="text" id="username" name="username" placeholder="请输入学号/工号" value="student" required>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="password">密码</label>
                    <input class="form-control" type="password" id="password" name="password" placeholder="请输入密码" required>
                </div>
                
                <button class="login-btn" type="submit">登 录</button>
            </form>
            
            <div class="login-links">
                <a href="javascript:void(0)" onclick="showModal('提示', '请联系管理员重置密码')">忘记密码</a>
                <a href="javascript:void(0)" onclick="alert('请联系管理员创建账号')">注册账号</a>
            </div>
        </div>
    </div>
    
    <footer>
        <p>© <?php echo date('Y'); ?> 简易报修系统 v1.0.0</p>
        <p>功能反馈: 15058593138@qq.com (手机号同微信)</p>
    </footer>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 切换用户类型
            var typeItems = document.querySelectorAll('.type-item');
            var usernameInput = document.getElementById('username');
            
            typeItems.forEach(function(item) {
                item.addEventListener('click', function() {
                    // 移除所有active类
                    typeItems.forEach(function(i) {
                        i.classList.remove('active');
                    });
                    
                    // 添加active类到当前项
                    this.classList.add('active');
                    
                    // 根据用户类型设置默认账号
                    var type = this.getAttribute('data-value');
                    switch (type) {
                        case '学生':
                            usernameInput.placeholder = '请输入学号';
                            usernameInput.value = 'student';
                            break;
                        case '后勤':
                            usernameInput.placeholder = '请输入工号';
                            usernameInput.value = 'houqin';
                            break;
                        case '系统':
                            usernameInput.placeholder = '请输入管理员账号';
                            usernameInput.value = 'admin';
                            break;
                    }
                });
            });
        });
        
        // 简单的模态框实现
        function showModal(title, content) {
            var modal = document.createElement('div');
            modal.style.position = 'fixed';
            modal.style.top = '0';
            modal.style.left = '0';
            modal.style.width = '100%';
            modal.style.height = '100%';
            modal.style.backgroundColor = 'rgba(0,0,0,0.5)';
            modal.style.display = 'flex';
            modal.style.justifyContent = 'center';
            modal.style.alignItems = 'center';
            modal.style.zIndex = '1000';
            
            var modalContent = document.createElement('div');
            modalContent.style.background = 'white';
            modalContent.style.padding = '20px';
            modalContent.style.borderRadius = '5px';
            modalContent.style.maxWidth = '400px';
            modalContent.style.width = '90%';
            
            var modalTitle = document.createElement('h3');
            modalTitle.textContent = title;
            modalTitle.style.marginBottom = '15px';
            
            var modalBody = document.createElement('div');
            modalBody.textContent = content;
            modalBody.style.marginBottom = '20px';
            
            var closeButton = document.createElement('button');
            closeButton.textContent = '关闭';
            closeButton.style.background = '#3f51b5';
            closeButton.style.color = 'white';
            closeButton.style.border = 'none';
            closeButton.style.padding = '8px 15px';
            closeButton.style.borderRadius = '3px';
            closeButton.style.cursor = 'pointer';
            closeButton.onclick = function() {
                document.body.removeChild(modal);
            };
            
            modalContent.appendChild(modalTitle);
            modalContent.appendChild(modalBody);
            modalContent.appendChild(closeButton);
            modal.appendChild(modalContent);
            document.body.appendChild(modal);
        }
    </script>
</body>
</html>
